import { createRouter, createWebHistory } from 'vue-router'

const cusColumn = () => import("@/views/CusColumn")
const Message = () => import("views/Message")
const utcCron = () => import("views/Time2utcCron")
const CheckOptions = () => import("views/CheckOptions")
const page404 =   () => import("views/404")
const routes = [
  {path:"/", redirect:"/message"},
  {
    path:"/cus-column",
    meta:{
      title:"Custom Column",
      role:"cus-column"
    },
    component:cusColumn
  },
  {
    path:"/message",
    meta:{
      title:"Custom Message",
      role:"cus-message"
    },
    component:Message
  },
  {
    path:"/utc-cron",
    meta:{
      title:"UTC Cron",
      role:"utc-cron"
    },
    component:utcCron
  },
  {
    path:"/check-options",
    meta:{
      title:"多选",
      role:"check-options"
    },
    component:CheckOptions
  },
  {
    path:"/404",
    meta:{
      isDefalut:true//不需要权限验证的页面
    },
    component:page404
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
router.beforeEach((to) => {
  // 页面title
  document.title = to.meta.title
    ? to.meta.title + " | vue3Component"
    : " vue3Component";
    // 路由权限列表, 在实际开发中应根据用户请求
  const roles = ["cus-message","cus-column","utc-cron","check-options"];
  // console.log(roles.includes(to.meta.role),to.meta.role)
  // 如果有权限或页面不需要权限, 路由跳转
  if(roles.includes(to.meta.role)||to.meta.isDefalut) {
    return true
  } else {
    router.push("/404")
  }
})
export default router